package org.geek4j.business.controller;

import java.util.Date;

import javax.validation.Valid;

import org.geek4j.business.repository.mybatis.domain.User;
import org.geek4j.business.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.DateTimeFormat.ISO;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author Geek4J
 * @since 2016年3月20日下午6:09:55
 * @description UserController
 */
@Controller
@RequestMapping("user")
public class UserController {

	@Autowired
	private UserService userService;

	/**
	 * @author Geek4J
	 * @since 2016年3月20日下午6:09:55
	 * @description find User By id
	 * @param id
	 * @return User JSON String
	 */
	@RequestMapping("findById")
	public ResponseEntity<User> findOne(Long id) {
		return ResponseEntity.ok(userService.findByIdWithRoles(id));
	}

	@RequestMapping(value = "validate")
	public ResponseEntity<String> validate(@Valid User user, BindingResult result) {
		if (result.hasErrors()) {
			return ResponseEntity.ok(result.getFieldError().getDefaultMessage());
		}
		return ResponseEntity.ok("格式正确");
	}

	@RequestMapping("date")
	public @ResponseBody String date(@DateTimeFormat(iso = ISO.DATE) Date date) {
		return "Converted date " + date;
	}
}